<table width="100%" border="1" rules="all" cellspacing="0" class="tblSort" cellpadding="0">
<thead>
  <tr>
    <th>Factura</th>
    <th colspan="2">Proveedor</th>
    <th>Referencia</th>
    <th>Creador el</th>
    <th>Creador por</th>
    <th>F.Factura</th>
    <th>Vencimiento</th>
    <th>Monto (inc/IGV)</th>
    <th>Local</th>
    <th>Saldo</th>
    </tr>
  </thead>
  <tbody>
<?php 
include('../../Connections/KRAPPERCON.php');
$oper = '';
$oper = getVal('oper');
$c_ini = getVal('t_c_ini');
$c_fin = getVal('t_c_fin');
$d_ini = getVal('t_d_ini');
$d_fin = getVal('t_d_fin');
$v_ini = getVal('t_v_ini');
$v_fin = getVal('t_v_fin');


$wh_c_ini = '';
$wh_c_fin = '';
$wh_d_ini = '';
$wh_d_fin = '';
$wh_v_ini = '';
$wh_v_fin = '';


$wh_where = '';
$prov = getVal('t_c_prov');
$ref = getVal('t_c_ref');
$sts = getVal('t_c_sts');

$usr = getVal('t_c_usr');
$loc = getVal('t_c_loc');

if ($oper = 'view'){

$wh_prov = isZeros($prov,' and a.ProveedID = '.$prov);
$wh_sts = isNull($sts,' and a.FactSts = '.$sts);
$wh_usr = isNull($usr," and a.UserID = '".$usr."'");
$wh_ref = isNull($ref," and a.FacturaRef = '".$ref."'");
$wh_local = isZeros($loc," and a.CentroID = ".$loc);

if (isEmpty($c_fin)){
$wh_c_ini = isNull($c_ini," and date(a.FactCompFeCr) = '".$c_ini."'");
}
else{
$wh_c_ini = isNull($c_ini," and date(a.FactCompFeCr) >= '".$c_ini."'");
$wh_c_fin = isNull($c_fin," and date(a.FactCompFeCr) <= '".$c_fin."'");
}

if (isEmpty($d_fin)){
$wh_d_ini = isNull($d_ini," and a.FactCompFeFc = '".$d_ini."'");
}
else{
$wh_d_ini = isNull($d_ini," and a.FactCompFeFc >= '".$d_ini."'");
$wh_d_fin = isNull($d_fin," and a.FactCompFeFc <= '".$d_fin."'");
}


if (isEmpty($v_fin)){
$wh_v_ini = isNull($d_ini," and a.FactFecVen = '".$v_ini."'");
}
else{
$wh_v_ini = isNull($v_ini," and a.FactFecVen >= '".$v_ini."'");
$wh_v_fin = isNull($v_fin," and a.FactFecVen <= '".$v_fin."'");
}

$where = $wh_prov.$wh_usr.$wh_ref.$wh_c_ini.$wh_c_fin.$wh_d_ini.$wh_d_fin.$wh_v_ini.$wh_v_fin.$wh_local.$wh_sts;

if (trim($where)!=''){
	$where = substr(ltrim($where),3);
	$where = ' where '.$where;
}

$a_id = '';
$a_nm = '';
$saldo = '';

$sql= 'select CentroID,CentroNom from tt_centro';
$res = $mysqli->query($sql);
if ($res){
	while($row = $res->fetch_assoc()){
		$a_id[] = $row['CentroID'];
		$a_nm[] = $row['CentroNom'];
	}
$res->close();
$mysqli->check_result();
}

$sql = 'SELECT
a.FactCompID,
a.FactCompFeCr,
a.FactCompFeFc,
a.UserID,
a.ProveedID,
a.FactFecVen,
a.FactSts,
a.CentroID,
a.FactImporte,
a.FactImpuesto,
a.FactTexto,
a.ClaDocID,
a.MoneyID,
a.FacturaRef,
b.ProveedNom,
b.ProveedNIF,
a.MonComp
FROM tt_facturacomp as a inner join tt_proveedor AS b
on a.ProveedID = b.ProveedID '.$where;
	doEcho($sql);
	$res = $mysqli->query($sql);
	if ($res){
		while($row = $res->fetch_assoc()){
		$local = $a_nm[array_search($row['CentroID'],$a_id)];
		$saldo = $row['FactImporte']-$row['MonComp'];
		?>
<tr>
<td><a href="#page/Procesos/MantFactura.php?oper=view&factura=<?php echo $row['FactCompID']; ?>"><?php echo $row['FactCompID']; ?></a></td>
<td><?php echo $row['ProveedNIF']; ?></td>
<td><?php echo $row['ProveedNom']; ?></td>
<td><?php echo $row['FacturaRef']; ?></td>
<td><?php echo $row['FactCompFeCr']; ?></td>
<td><?php echo $row['UserID']; ?></td>
<td><?php echo $row['FactCompFeFc']; ?></td>
<td><?php echo $row['FactFecVen']; ?></td>
<td class="qt"><?php echo rnd($row['FactImporte']); ?></td>
<td><?php echo $local; ?></td>
<td class="qt"><?php echo rnd($saldo); ?></td>
</tr>        
        <?php 
		}
	$res->close();
	$mysqli->check_result();
	}
}
/*
	oper : 'view',
	t_c_ini : GEBI('t_cf_cini').value,
	t_c_fin : GEBI('t_cf_cfin').value,
	t_d_ini : GEBI('t_cf_dini').value,
	t_d_fin : GEBI('t_cf_dfin').value,
	t_v_ini : GEBI('t_cf_vini').value,
	t_v_fin : GEBI('t_cf_vfin').value,
	t_c_prov : GEBI('cbo_cf_prov').value,
	t_c_ref : GEBI('t_cf_ref').value,
	t_c_sts : GEBI('cbo_cf_sts').value,
	t_c_usr : GEBI('t_cf_usr').value,
	t_c_loc : GEBI('cbo_cf_local').value
	*/
?>
  </tbody>
</table>
<script type="text/javascript">
function pagFactCompra(fac){
	sendData('Procesos/Opers/OperFact.php',
	{
		check : 'X',
		oper : 'pago',
		factura : fac
	});
}

function viewFacturaC(obj){
	var f = $(obj).html();
	alert(f);
	loadPage('Procesos/MantFactura.php',{
		oper : 'view',
		factura : f
	});
}
</script>
